﻿using DemountCIM.Framework.DataBase;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WFDataBase;
using static System.Formats.Asn1.AsnWriter;

namespace DemountCIM.Framework.DataBase
{
    public class DataBaseContainer
    {
        private static DataBaseContainer container;                                 // 全局对象

        private SqlSugarScope scope;
        private IParameterRepository parameterRepository;
        private IUserRepository userRepository;
        private IProcessRepository processRepository;
        private IPlateBufferRepository plateBufferRepository;

        public DataBaseContainer()
        {
            DesktopScope.SetInstance(new ConnectionConfig
            {
                DbType = SqlSugar.DbType.MySql,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                ConnectionString = "Server=127.0.0.1;Database=demounter;Port=3306;Uid=root;Pwd=123456;"
            }, SqlLog);
            scope = DesktopScope.GetInstance();

            parameterRepository = new ParameterRepository(scope);
            userRepository = new UserRepository(scope);
            processRepository = new ProcessRepository(scope);
            plateBufferRepository = new PlateBufferRepository(scope);
        }

        /// <summary>
        /// 获取全局对象
        /// </summary>
        /// <returns></returns>
        public static DataBaseContainer GetInstance()
        {
            if (null == container)
            {
                container = new DataBaseContainer();
            }
            return container;
        }

        /// <summary>
        /// 获取数据库连接状态
        /// </summary>
        /// <returns></returns>
        public bool GetConnection()
        {
            return scope.Ado.IsValidConnection();
        }

        public IParameterRepository GetParameterRepository()
        {
            return parameterRepository;
        }

        public IUserRepository GetUserRepository()
        {
            return userRepository;
        }

        public IProcessRepository GetProcessRepository()
        {
            return processRepository;
        }

        public IPlateBufferRepository GetPlateBufferRepository()
        {
            return plateBufferRepository;
        }

        /// <summary>
        /// sql日志
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        private void SqlLog(string sql, SugarParameter[] parameters)
        {

        }
    }
}
